<template>
  <el-dialog
    :title="operType == 'add' ? '新增收款信息' : operType == 'detail' ?  '查看收款信息' : '修改收款信息'"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    width="800px"
    v-dialogDrag="true"
    @open="handleDialogOpen"
    @closed="handleDialogClosed"
  >
    <el-form :model="formData" :disabled="operType == 'detail'" :rules="rules" ref="addEditPaymentForm" size="small" label-width="80px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="单位名称" prop="unitName">
            <el-input
              v-model="formData.unitName"
              placeholder="请输入"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收款账户" prop="paymentAccount">
            <el-input
              v-model="formData.paymentAccount"
              placeholder="请输入"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div slot="footer">
      <el-button type="primary" v-if="operType != 'detail'" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import { getPaymentInfo, addPaymentInfo, editPaymentInfo } from "@/api/person/payment"
  export default {
    name: '',
    components: {},
    props: {
      show: {
        type: Boolean,
        default: false,
      },
      operType: {
        type: String,
        default: 'add',
      },
      row: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      return {
        formData: {},
        rules: {
          unitName: [
            { required: true, message: '请选择', trigger: 'change' }
          ],
          paymentAccount: [
            { required: true, message: '请选择', trigger: 'change' }
          ],
        }
      }
    },
    computed: {
      dialogVisible: {
        get() {
          return this.show
        },
        set(value) {
          this.$emit('update:show', value)
        }
      }
    },
    created() {
      this.initFormData()
    },
    methods: {
      // 弹框打开时
      handleDialogOpen() {
        if (this.operType == 'edit' || this.operType == 'detail') {
          getPaymentInfo(this.row.paymentInfoId).then(res => {
            this.formData = {
              ...this.formData,
              ...(res.data || {})
            }
          }).catch(err => {})
        }
      },
      // 弹框关闭时
      handleDialogClosed() {
        this.initFormData()
        this.$refs.addEditPaymentForm.resetFields()
        this.attachmentFileList = []
      },
      // 初始化表单数据
      initFormData() {
        this.formData = {
          paymentInfoId: '', // 收款信息id
          unitName: '', // 单位名称
          paymentAccount: '', // 收款账户
        }
      },
      submitForm() {
        this.$refs.addEditPaymentForm.validate(valid => {
          if (!valid) return
          let fn = null
          if (this.operType == 'add') fn = addPaymentInfo
          else if (this.operType == 'edit') fn = editPaymentInfo
          fn(this.formData).then(res => {
            this.$message.success('操作成功')
            this.dialogVisible = false
            this.$emit('refresh')
          }).catch(err => {})
        })
      }
    }
  }
</script>
